package com.coin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coin.domain.Coin;
import com.coin.dto.CoinDto;
import com.coin.mapper.CoinMapper;
import com.coin.service.CoinService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 李力
 * @description 针对表【coin(币种配置信息)】的数据库操作Service实现
 * @createDate 2025-06-12 10:12:52
 */
@Service
public class CoinServiceImpl extends ServiceImpl<CoinMapper, Coin>
        implements CoinService {

    /**
     * @Description: 分页条件查询数字货币
     * @Author: 李 力
     * @Date: 2025/6/12
     * @Param:
     * @return: R
     */
    @Override
    public Page<Coin> findByPage(String name, String type, Byte status, String title, String walletType, Page<Coin> page) {
        LambdaQueryWrapper<Coin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.like(StrUtil.isNotEmpty(name), Coin::getName, name)
                .eq(StrUtil.isNotEmpty(type), Coin::getType, type)
                .eq(status != null, Coin::getStatus, status)
                .like(StrUtil.isNotEmpty(title), Coin::getTitle, title)
                .like(StrUtil.isNotEmpty(walletType), Coin::getWallet, walletType);
        return page(page, queryWrapper);
    }

    /**
     * @Description: CoinServiceImpl
     * @Author: 李 力
     * @Date: 2025/6/12
     * @Param:
     * @return:
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean setStatus(Coin coin) {
        if (coin == null) {
            throw new RuntimeException("参数为空");
        }
        Coin data = getById(coin.getId());
        if (data == null) {
            throw new RuntimeException("查无此币种");
        }
        return updateById(coin);
    }

    /**
     * @Description: 查询币种的详细信息
     * @Author: 李 力
     * @Date: 2025/6/12
     * @Param:
     * @return: R
     */
    @Override
    public Coin info(Long id) {
        if (id == null) {
            throw new RuntimeException("参数为空");
        }
        return getById(id);
    }

    /**
     * @Description: 通过状态查询所有的币种信息
     * @Author: 李 力
     * @Date: 2025/6/12
     * @Param:
     * @return: R
     */
    @Override
    public List<Coin> getCoinsByStatus(Byte status) {
        LambdaQueryWrapper<Coin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(status != null, Coin::getStatus, status);
        return list(queryWrapper);
    }

    /**
     * @Description: 修改币种的信息
     * @Author: 李 力
     * @Date: 2025/6/12
     * @Param:
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCoin(Coin coin) {
        if (coin == null) {
            throw new RuntimeException("参数为空");
        }
        return updateById(coin);
    }

    @Override
    public Coin getCoinByCoinName(String coinName) {
        LambdaQueryWrapper<Coin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(StrUtil.isNotEmpty(coinName), Coin::getName, coinName);
        return getOne(queryWrapper);
    }

    /**
     * @Description: 远程调用接口
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return:
     */
    @Override
    public List<CoinDto> coinService(List<Long> coinIds) {
        List<Coin> coins = listByIds(coinIds);
        if (coins == null || coins.isEmpty()) {
            return null;
        }
        List<CoinDto> list = new ArrayList<>();
        for (Coin coin : coins) {
            CoinDto coinDto = new CoinDto();
            BeanUtil.copyProperties(coin, coinDto);
            list.add(coinDto);
        }
        return list;
    }
}




